// pages/daixiaoDingdan/shenheAndfahuo/shenheAndfahuo.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flagIndex: 0, //1待发货0待审核2待收货3待结算4待处理5待评价6已完成
    dingdanid: '',
    resshopid: '',
    dingdaninfo: '',
    wuliuinfo:''
  },
  wuliuinfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getExpressInfo",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        orderId: that.data.dingdanid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          setTimeout(function () {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          that.setData({
            wuliuinfo: res.data.bean,
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  // 取消代销
  cancel(){
    let that = this
    wx.showModal({
      title: '是否取消代销',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "cancelOrder",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              orderId: that.data.dingdanid,
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1500)
              }
            }
          });
        } else {
          console.log('点击取消回调')
        }
      }
    })
  },
  // 去商品详情
  togoodsxq(e) {
    let that = this
    let goodsid = e.currentTarget.dataset.goodsid
    wx.navigateTo({
      url: '/pages/goods/page/goodsXiangqing/daixiaoxianshiGoods/daixiaoxianshiGoods?goodsid=' + goodsid + '&flag=' + that.data.dingdaninfo.status,
    })
  },
  // 查看物流
  toWuliu() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/wuliuXinxi/wuliuXinxi?id=' + that.data.dingdanid,
    })
  },
  finishsell() {
    let that = this
    wx.showModal({
      title: '是否结束代销',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "closingSale",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              orderId: that.data.dingdanid
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                setTimeout(function () {
                  wx.hideLoading()
                }, 0)
                setTimeout(function () {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
                setTimeout(function () {
                  wx.showLoading({
                    title: '加载中',
                  })
                  that.getdingdaninfo()
                }, 1500)

              }
            }
          });
        } else {
          console.log('点击取消回调')
        }
      }
    })

  },
  // 去评价
  toPingjia() {
    let that = this
    wx.navigateTo({
      url: '/pages/setting/page/pingjiaDianpu/pingjiaDianpu?shopid=' + that.data.resshopid + '&shopname=' + that.data.dingdaninfo.shopname + '&goodsid=' + that.data.dingdaninfo.goodsList[0].goodsId + '&shopimg=' + that.data.dingdaninfo.shoplogo + '&dingdanid=' + that.data.dingdanid,
    })
  },
  tochaoshi() {
    let that = this
    let type = 1
    if (that.data.dingdaninfo.trtime){
      type = 2
    }
    wx.navigateTo({
      url: '/pages/setting/page/dianpu/daixiaodianpu/daixiaodianpu?dingdanid=' + that.data.dingdanid + '&resshopid=' + that.data.resshopid + '&type=' + type,
    })
  },
  getdingdaninfo() {
    let that = this
    wx.request({
      url: app.globalData.myurl,
      data: {
        cmd: "getAgtOrderDetail",
        shopId: wx.getStorageSync('shopId'),
        userId: wx.getStorageSync('userId'),
        id: that.data.dingdanid
      },
      header: {
        "content-type": "application/x-www-form-urlencoded",
        "token": wx.getStorageSync('token')
      },
      success(res) {
        if (res.data.zjstate == 1) {
          setTimeout(function () {
            wx.hideLoading()
            wx.stopPullDownRefresh() //停止下拉刷新
          }, 0)
          setTimeout(function () {
            wx.showModal({
              title: '您的账号已在其他设备登录，请重新登录！',
              showCancel: false, //是否显示取消按钮false去掉取消按钮
              success: function (res) {
                if (res.confirm) {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                } else {
                  wx.clearStorageSync()
                  wx.reLaunch({
                    url: '/pages/login/login',
                  })
                }
              }
            })
          }, 0)
          return
        }
        console.log(res)
        if (res.data.result == 1) {
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
          setTimeout(function() {
            wx.showToast({
              title: res.data.resultNote,
              icon: 'none',
              duration: 1500
            })
          }, 0)
        } else {
          that.setData({
            dingdaninfo: res.data.orderBean,
            resshopid: res.data.orderBean.shopid
          })
          setTimeout(function() {
            wx.hideLoading()
          }, 0)
        }
      }
    });
  },
  // 是同意代销
  consentsell() {
    let that = this
    wx.showModal({
      title: '是否同意代销',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "toExamine",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              orderId: that.data.dingdanid,
              checkState: 0
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
                setTimeout(function() {
                  wx.showLoading({
                    title: '加载中',
                  })
                  that.getdingdaninfo()
                }, 1500)
              }
            }
          });
        } else {
          console.log('点击取消回调')
        }
      }
    })
  },
  // 是否拒绝代销
  refusesell() {
    let that = this
    wx.showModal({
      title: '是否拒绝代销',
      success: function(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.myurl,
            data: {
              cmd: "toExamine",
              shopId: wx.getStorageSync('shopId'),
              userId: wx.getStorageSync('userId'),
              orderId: that.data.dingdanid,
              checkState: 1
            },
            header: {
              "content-type": "application/x-www-form-urlencoded",
              "token": wx.getStorageSync('token')
            },
            success(res) {
              if (res.data.zjstate == 1) {
                setTimeout(function () {
                  wx.hideLoading()
                  wx.stopPullDownRefresh() //停止下拉刷新
                }, 0)
                setTimeout(function () {
                  wx.showModal({
                    title: '您的账号已在其他设备登录，请重新登录！',
                    showCancel: false, //是否显示取消按钮false去掉取消按钮
                    success: function (res) {
                      if (res.confirm) {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      } else {
                        wx.clearStorageSync()
                        wx.reLaunch({
                          url: '/pages/login/login',
                        })
                      }
                    }
                  })
                }, 0)
                return
              }
              console.log(res)
              if (res.data.result == 1) {
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
              } else {
                setTimeout(function() {
                  wx.hideLoading()
                }, 0)
                setTimeout(function() {
                  wx.showToast({
                    title: res.data.resultNote,
                    icon: 'none',
                    duration: 1500
                  })
                }, 0)
                setTimeout(function () {
                  wx.navigateBack({
                    delta: 1
                  })
                }, 1500)
              }
            }
          });
        } else {
          console.log('点击取消回调')
        }
      }
    })
  },
  // 发货
  toFahuo() {
    let that = this
    wx.navigateTo({
      url: '/pages/order/page/qufahuo/qufahuo/qufahuo?id=' + that.data.dingdanid,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let that = this
    that.setData({
      dingdanid: options.dingdanId
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let that = this
    setTimeout(function() {
      wx.showLoading({
        title: '加载中',
      })
    }, 0)
    that.getdingdaninfo()
    that.wuliuinfo()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})